<?php

namespace App\Member\Controllers;

use App\Admin\Repositories\UserBank;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Models\UserBank as UserBankModel;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;


class UserBankController extends BaseController
{
    //protected $description = '我的银行卡';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserBank(), function (Grid $grid) {
            $grid->filter(function($filter){
                // 展开过滤器
                //$filter->expand();

                // 在这里添加字段过滤器
                $filter->equal('id', '产品序列号');
                $filter->like('name', 'name');
                $filter->date('created_at', 'name');

            });

            $grid = $this->gridButton($grid, false, false, true, false);
            $grid->setActionClass(Grid\Displayers\ContextMenuActions::class);
            $grid->model()->where('user_id', $this->getUser()->id)->where('type', 1);

            $grid->rows(function($collection){
                foreach ($collection as $k=>$v){
                    $v->key = $k+1;
                }
                return $collection;
            });
            $grid->key(trans('user-bank.fields.sort'));
            $grid->sfz_zimg->image('', '', 30);
            $grid->sfz_fimg->image('', '', 30);
            $grid->sfz_id;
            $grid->bank_number;
            $grid->bank_type;
            $grid->swift_code;
            $grid->status()->using(
                [UserBankModel::$status[0],
                    UserBankModel::$status[1],
                    UserBankModel::$status[2]]
            )->label(['danger','primary','success']);
            $grid->created_at();
            $grid->updated_at()->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }


    /***
     *  重构显示页面 用Tab 方式显示多页面
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('user-bank.labels.user_bank_2'))
            // body 方法可以接受 Laravel 的视图作为参数
            ->row(function (Row $row) use ($id) {
                $row->column(6, function (Column $column) use ($id)  {
                    $column->append($this->detail($id));
                });
            });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserBank(), function (Show $show) {

            $show = $this->showButton($show);

            $show->sfz_id;
            $show->sfz_zimg->image('', '', 30);
            $show->sfz_fimg->image('', '', 30);
            $show->bank_number;
            $show->bank_type;
            $show->swift_code;
            $show->status(trans('user-bank.fields.status'))->using(
                [UserBankModel::$status[0],
                    UserBankModel::$status[1],
                    UserBankModel::$status[2]]
            )->dot(['danger','primary','success']);
            $show->created_at;
            $show->updated_at;
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserBank(), function (Form $form) {
            $form = $this->formButton($form);

            $form->tab(trans('handlers.admin_user_yhxxxxxx'),function($form){
                $form->text('bank_number')->required();
                $form->text('bank_type')->required();
                $form->text('bank_address')->required();
                $form->text('swift_code');
                $form->text('sfz_id')->required();
                $form->image('sfz_zimg')->autoUpload()->required()->accept('jpg,png,jpeg');
                $form->image('sfz_fimg')->autoUpload()->required()->accept('jpg,png,jpeg');
            });
//            ->tab('银行卡信息', function($form){
//
//            });
            $form->hidden('user_id');
            $form->hidden('type');
            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function($form){
                $form->user_id  = $this->getUser()->id;
                $form->type     = 1;
                $result = UserBankModel::where('user_id', $form->user_id)->where('type', 1)->get()->pluck('status')->toArray();
                if (in_array(1, $result)){
                    return $form->error(trans('handlers.admin_user_shzbkcftj'));
                }
            });
        });
    }


    /**
     * Create interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->title('创建客户资料')
            ->description(' ')
            ->body(function ($row) {
//                $row->column(4, new TotalAdminUsers());
//                $row->column(4, new NewUsers());
//                $row->column(4, new NewDevices());
            })
            ->row(function (Row $row) {
                $row->column(6, function (Column $column){
                    $column->append($this->form());
                });
            });
    }




}
